// 权限控制

console.log('执行文件')

import router from "./router"
import { getToken } from "./utils/auth"
import NProgress from 'nprogress'  // 进度条展示
import 'nprogress/nprogress.css'
import store from "./store"
const WGITE_LIST = ['/login', '/404']   // 白名单


router.beforeEach((to, from, next) => {
  NProgress.start() // 进度条开始
  const token = getToken()
  if (token) {
    // 有token
    // 是否去登录页
    if (to.path === '/login') {
      next('/')
    } else {
      next()
      // 优化接口请求次数
      if (!store.state.user.userInfo.userId) {
        store.dispatch('user/fetUserInfo')
      }
    }
  } else {
    // 判断是否白名单
    if (WGITE_LIST.includes(to.path)) {
      next()
    } else {
      next('/login')
    }
  }
  NProgress.done()  // 进度条结束
})
